import axios from "axios";
import store from "@/store";
import Qs from "qs";

const request = axios.create({
    baseURL: 'http://localhost:8080',
    timeout: 10000
})

request.interceptors.request.use(config => {
    if (config.method === 'put' || config.method === 'post') {
        config.headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        config.data = Qs.stringify(config.data)
    }
    console.log('请求拦截器')
    console.log(config)
    //设置token
    if (store.state.token !== '') {
        config.headers['Authorization'] = store.state.tokenHead + ' ' + store.state.token
    }
    return config
})

request.interceptors.response.use(res => {
    console.log(res)
    return res['data']
})


export default request
